﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using upcdew_20111_group2b.CapaNegocio;
using upcdew_20111_group2b.CapaDominio;

namespace upcdew_20111_group2b.Controllers
{
    public class ResidenteController : Controller
    {
        //
        // GET: /Residente/
        public AdminCondominio AdminCondominio{ get; set; }

        public ActionResult Index()
        {
            ICollection<Residente> modelo = AdminCondominio.ListarTodosLosResidentes();
            return View(modelo);
        }

        //
        // GET: /Residente/Details/5

        public ActionResult Details(String codresidente)
        {
            Residente modelo = AdminCondominio.ObtenerResidente(codresidente);
            return View(modelo);
        }

        //
        // GET: /Residente/Create

        public ActionResult Create()
        {
            return View();
        } 

        //
        // POST: /Residente/Create

        [HttpPost]
        public ActionResult Create(FormCollection collection)
        {
            try
            {
                if (ModelState.IsValid) {
                    Residente residenteCrear = new Residente();
                    residenteCrear.CodResidente = (String)collection["CodResidente"];
                    residenteCrear.Nom_Residente = (String)collection["Nom_Residente"];
                    residenteCrear.Fec_Nacimiento = DateTime.Parse(collection["Fec_Nacimiento"]);
                    residenteCrear.Tex_Correo = (String)collection["Tex_Correo"];
                    residenteCrear.Tex_Clave = (String)collection["Tex_Clave"];
                    residenteCrear.Tex_Dni = (String)collection["Tex_Dni"];
                    residenteCrear.Tex_Tipo_Residente = (String)collection["Tex_Tipo_Residente"];
                    AdminCondominio.CrearResidente(residenteCrear);
                    return RedirectToAction("Index");
                }
                else
                {
                    return View();
                }                                
            }
            catch (Exception e)
            {
                ModelState.AddModelError(String.Empty, "Error al crear el residente: " + e.Message);
                return View();
            }
        }
        
        //
        // GET: /Residente/Edit/5        
        public ActionResult Edit(String codresidente)
        {
            Residente modelo = AdminCondominio.ObtenerResidente(codresidente);
            return View(modelo);
        }

        //
        // POST: /Residente/Edit/5        
        [HttpPost]
        public ActionResult Edit(String codresidente, FormCollection collection)
        {
            try
            {
                Residente residenteModificar = AdminCondominio.ObtenerResidente(codresidente);
                residenteModificar.Nom_Residente = (String)collection["Nom_Residente"];
                residenteModificar.Fec_Nacimiento = DateTime.Parse(collection["Fec_Nacimiento"]);
                residenteModificar.Tex_Correo = (String)collection["Tex_Correo"];
                residenteModificar.Tex_Clave = (String)collection["Tex_Clave"];
                residenteModificar.Tex_Dni = (String)collection["Tex_Dni"];
                residenteModificar.Tex_Tipo_Residente = (String)collection["Tex_Tipo_Residente"];
                AdminCondominio.ModificarResidente(residenteModificar);
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /Residente/Delete/5        

        public ActionResult Delete(String codresidente)
        {
            Residente modelo = AdminCondominio.ObtenerResidente(codresidente);
            return View(modelo);
        }
        
        // POST: /Residente/Delete/5        
        [HttpPost]
        public ActionResult Delete(String codresidente, FormCollection collection)
        {
            try
            {
                // TODO: Add delete logic here
                Residente residenteEliminar = AdminCondominio.ObtenerResidente(codresidente);
                AdminCondominio.EliminarResidente(residenteEliminar);
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
    }
}
